package com.ymttest.business.service;

import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;

import com.google.gson.JsonObject;
import com.shop2cn.iapi.grouponmgr.parameter.*;
import com.shop2cn.iapi.grouponmgr.service.*;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymttest.database.mongo.Shop2cnSellerPromotionInfoWapper;
import com.ymttest.utils.YMTDateUtil;
public class Shop2cnGrouponCallService {
	/** 初始化创建单规格拼团bean
	 * @param mchid 商家id
	 * @param pid 商品id
	 * @param catalogId 规格id
	 * @param grouponPrice 拼团价格
	 * @return
	 */
	public static SaveGrouponReq initSaveGrouponReq(Integer mchid, String pid ,String catalogId,Double grouponPrice) {
		SaveGrouponReq savegrouponBean=new SaveGrouponReq();
		List<GrouponCatalogDto> grouponCatalogs = new ArrayList<GrouponCatalogDto>();
		GrouponCatalogDto e = new GrouponCatalogDto();
		e.setCatalogId(catalogId);
		e.setGrouponPrice(BigDecimal.valueOf(grouponPrice));
		grouponCatalogs.add(e);
		savegrouponBean.setGrouponCatalogs(grouponCatalogs);
		savegrouponBean.setProductId(pid);
		savegrouponBean.setSellerId(mchid);
		savegrouponBean.setGrouponMaxHour(2);
		savegrouponBean.setGrouponMemberCount(2);
		savegrouponBean.setBeginTime(YMTDateUtil.getBeforeOrNextDay(-1,"yyyy-MM-dd HH:mm:ss"));
		savegrouponBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(1,"yyyy-MM-dd HH:mm:ss"));
		return savegrouponBean;
	}
	
	/** 初始化创建多规格拼团bean
	 * @param mchid 商家id
	 * @param pid 商品id
	 * @param catalogId 规格id
	 * @param grouponPrice 拼团价格
	 * @return
	 */
	public static SaveGrouponReq initSaveGrouponReq(Integer mchid, String pid ,List<String> catalogs,Double grouponPrice) {
		SaveGrouponReq savegrouponBean=new SaveGrouponReq();
		List<GrouponCatalogDto> grouponCatalogs = new ArrayList<GrouponCatalogDto>();
		for (String catalogId : catalogs) {
			GrouponCatalogDto e = new GrouponCatalogDto();
			e.setCatalogId(catalogId);
			e.setGrouponPrice(BigDecimal.valueOf(grouponPrice));
			grouponCatalogs.add(e);
		}
	
		savegrouponBean.setGrouponCatalogs(grouponCatalogs);
		savegrouponBean.setProductId(pid);
		savegrouponBean.setSellerId(mchid);
		savegrouponBean.setGrouponMaxHour(2);
		savegrouponBean.setGrouponMemberCount(2);
		savegrouponBean.setBeginTime(YMTDateUtil.getBeforeOrNextDay(0,"yyyy-MM-dd HH:mm:ss"));
		savegrouponBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(1,"yyyy-MM-dd HH:mm:ss"));
		return savegrouponBean;
	}
	/**
	 * 创建拼团
	 */
	public static Integer SaveGrouponCall(SaveGrouponReq savegrouponBean) throws ClientProtocolException, IOException, InterruptedException {
		SaveGrouponCall savegrouponCall=new SaveGrouponCall();
		savegrouponCall.setData(savegrouponBean);
		savegrouponCall.callService();			
		return savegrouponCall.getJsonObject().get("data").getAsInt();
	}
	
	public static void main(String[] args) throws Exception {

	}
	
	/**
	 * 删除拼团
	 * @param grouponProductId
	 * @param sellerId
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	public static void DeleteGroupon(Integer grouponProductId,Integer sellerId) throws ClientProtocolException, IOException{
		DeleteGrouponBean bean = new DeleteGrouponBean();
		DeleteGrouponCall call = new DeleteGrouponCall();
		bean.setGrouponProductId(grouponProductId);
		bean.setSellerId(sellerId);
		call.setData(bean);
		call.callService();
		Logger.verifyEquals("200", call.getString("code"), "验证code");
		
	}
	
	
	/**
	 * 删除拼团
	 * @param grouponProductId
	 * @param sellerId
	 * @return 
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	public static JsonObject GrouponProductInfo(Integer sellerId,Integer grouponProductId) throws ClientProtocolException, IOException{
		GrouponProductInfoBean grouponProductInfoBean = new GrouponProductInfoBean();
		GrouponProductInfoCall grouponProductInfoCall = new GrouponProductInfoCall();
		grouponProductInfoBean.setGrouponProductId(grouponProductId);
		grouponProductInfoBean.setSellerId(sellerId);
		grouponProductInfoCall.setData(grouponProductInfoBean);
		grouponProductInfoCall.callService();
		Logger.verifyEquals("200", grouponProductInfoCall.getString("code"), "验证code");
		return grouponProductInfoCall.getJsonObject().getAsJsonObject("data");
	}
	
	
}
